package com.harmony.pilot.generation.controller;

import com.harmony.pilot.common.dto.GenerationRequest;
import com.harmony.pilot.common.dto.RestResult;
import com.harmony.pilot.generation.service.GenerationService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 代码生成控制器
 */
@Slf4j
@RestController
@RequestMapping("/generation")
@RequiredArgsConstructor
public class GenerationController {
    
    private final GenerationService generationService;
    
    /**
     * 生成代码
     */
    @PostMapping("/generate")
    public RestResult<String> generateCode(@Valid @RequestBody GenerationRequest request) {
        log.info("Received code generation request: type={}, prompt={}",
                request.getGenerationType(), request.getPrompt());
        
        try {
            String code = generationService.generateCode(request);
            return RestResult.success(code);
        } catch (Exception e) {
            log.error("Failed to generate code", e);
            return RestResult.error("Failed to generate code: " + e.getMessage());
        }
    }
    
    /**
     * 生成函数
     */
    @PostMapping("/function")
    public RestResult<String> generateFunction(@Valid @RequestBody GenerationRequest request) {
        log.info("Received function generation request");
        
        try {
            String code = generationService.generateFunction(request);
            return RestResult.success(code);
        } catch (Exception e) {
            log.error("Failed to generate function", e);
            return RestResult.error("Failed to generate function: " + e.getMessage());
        }
    }
    
    /**
     * 生成类
     */
    @PostMapping("/class")
    public RestResult<String> generateClass(@Valid @RequestBody GenerationRequest request) {
        log.info("Received class generation request");
        
        try {
            String code = generationService.generateClass(request);
            return RestResult.success(code);
        } catch (Exception e) {
            log.error("Failed to generate class", e);
            return RestResult.error("Failed to generate class: " + e.getMessage());
        }
    }
    
    /**
     * 生成单元测试
     */
    @PostMapping("/test")
    public RestResult<String> generateTest(@Valid @RequestBody GenerationRequest request) {
        log.info("Received test generation request");
        
        try {
            String code = generationService.generateTest(request);
            return RestResult.success(code);
        } catch (Exception e) {
            log.error("Failed to generate test", e);
            return RestResult.error("Failed to generate test: " + e.getMessage());
        }
    }
    
    /**
     * 生成注释
     */
    @PostMapping("/comment")
    public RestResult<String> generateComment(@Valid @RequestBody GenerationRequest request) {
        log.info("Received comment generation request");
        
        try {
            String comment = generationService.generateComment(request);
            return RestResult.success(comment);
        } catch (Exception e) {
            log.error("Failed to generate comment", e);
            return RestResult.error("Failed to generate comment: " + e.getMessage());
        }
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public RestResult<String> health() {
        return RestResult.success("Code Generation Service is running");
    }
}

